package com.tanhua.dubbo.api;

import com.tanhua.api.RecommendUserApi;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public RecommendUser findWithMaxScore(Long toUserId) {
        //根据toUserId查询，根据评分score排序，获取第一条
        //构建Criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //构建Query对象
        Query query = new Query(criteria)
                .with(Sort.by(Sort.Order.desc("score")))
                .limit(1);

        //调用mongoTemplate查询
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);

        //如果为空，则直接返回结果
        if (recommendUser == null) {
            //如果为空，则查询缘分值最高的用户
            query = new Query()
                    .with(Sort.by(Sort.Order.desc("score")))
                    .limit(1);
            recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        }

        return recommendUser;
    }

    @Override
    public List<RecommendUser> findRecommendUser(Long myUserId) {
        Query query = new Query(Criteria.where("toUserId").is(myUserId));
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);
        return recommendUserList;
    }

    @Override
    public RecommendUser findRecommendById(Long id, Long getid) {
        Query query = new Query(Criteria.where("userId").is(id).and("toUserId").is(getid));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if (recommendUser == null) {
            recommendUser=new RecommendUser();
            recommendUser.setScore(90d);
            recommendUser.setUserId(id);
            recommendUser.setToUserId(getid);


        }
        return recommendUser;
    }

    @Override
    public List<RecommendUser> findRecommendUserById(Long getid) {
        Query query = new Query(Criteria.where("userId").is(getid));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<Long> list = userLikes.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());

        Criteria criteria = Criteria.where("toUserId").is(getid).and("userId").nin(list);
        TypedAggregation typedAggregation=TypedAggregation.newAggregation(RecommendUser.class,  Aggregation.match(criteria),//查询条件
                Aggregation.sample(10) );
        AggregationResults<RecommendUser> aggregate = mongoTemplate.aggregate(typedAggregation, RecommendUser.class);

        return aggregate.getMappedResults();
    }
}
